
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TurnOrArtifactPanel : BasePanel
{
    [Header("按钮")]
    public Button closeButton;
    public Button upButton;
    public Button downButton;
    [Header("名称")]
    public Text itemName;//看是符咒还是神器
    public string currName;
    public int currSlot = 0;//如果是符咒，0代表第一个格子，1代表第二个格子
    [Header("Main")]
    public Text nameText;
    public Image iconImage;
    public Text desText;
    [Header("列表")]
    public Transform listParent;
    public GameObject slot;
    //格子列表
    public List<GameObject> slotList = new List<GameObject>();
    [Header("当前的符咒或神器名称")]
    public string currItemName;
    [Header("打开的主格子")]
    public TurnArtifactMainSlot mainSlot;

    public override void OnClose()
    {
        base.OnClose();

        //操作
        GameManager.Instance.canOprate = true;

        EventManager.ReMsgDic("TurnOrArtifactMainMsg", UpdateMainData);
    }

    public override void OnInit()
    {
        base.OnInit();
        path = "UI/Panel/TurnOrArtifactPanel";
        type = PanelType.Panel;

        //操作
        GameManager.Instance.canOprate = false;

        EventManager.AddMsgDic("TurnOrArtifactMainMsg", UpdateMainData);
    }

    public override void OnShow(params object[] par)
    {
        base.OnShow(par);

        SeekUI();
        JudgeArtifactTurn(par);
    }
    //寻找UI
    public void SeekUI()
    {
        closeButton = item.transform.Find("Close").GetComponent<Button>();
        upButton = item.transform.Find("Main/Up").GetComponent<Button>();
        downButton = item.transform.Find("Main/Down").GetComponent<Button>();
        itemName = item.transform.Find("Name").GetComponent<Text>();
        nameText = item.transform.Find("Main/Name").GetComponent<Text>();
        iconImage = item.transform.Find("Main/Icon").GetComponent<Image>();
        desText = item.transform.Find("Main/Des").GetComponent<Text>();
        listParent = item.transform.Find("List/Viewport/Content");
        slot = listParent.Find("Slot").gameObject;

        //关闭
        slot.SetActive(false);

        //按钮赋值
        closeButton.onClick.AddListener(CloseButton);
        upButton.onClick.AddListener(UpButton);
        downButton.onClick.AddListener(DownButton);
    }
    //打开第一个
    public void OpenListOne()
    {
        if(currName=="神器")
        {
            //如果没有装备
            if(ArtifactManager.instance.currArtifact=="")
            {
                //选择第一个打开
                ChooseOneOpen();
            }
            else
            {
                ChooseOpenOther(ArtifactManager.instance.currArtifact);
            }
        }
        else
        {
            if(currSlot==0)
            {
                if (TurnManager.instance.currOne == "")
                {
                    ChooseOneOpen();
                }
                else
                {
                    ChooseOpenOther(TurnManager.instance.currOne);
                }
            }
            else
            {
                if (TurnManager.instance.currTow == "")
                {
                    ChooseOneOpen();
                }
                else
                {
                    ChooseOpenOther(TurnManager.instance.currTow);
                }
            }
        }
    }
    //选择第一个打开
    public void ChooseOneOpen()
    {
        slotList[0].GetComponent<TurnArtifactSlot>().SetChoose();
        //获取数据
        TurnOrArtifactMainMsg msg = new TurnOrArtifactMainMsg();
        if(currName=="神器")
        {
            msg.aData = ArtifactManager.instance.GetArtifact(slotList[0].GetComponent<TurnArtifactSlot>().myName);
        }
        else
        {
            msg.tData = TurnManager.instance.GetTurn(slotList[0].GetComponent<TurnArtifactSlot>().myName);
        }
        EventManager.AddMsg(msg);
    }
    //打开对应装备的
    public void ChooseOpenOther(string _name)
    {
        foreach(var i in slotList)
        {
            TurnArtifactSlot t = i.GetComponent<TurnArtifactSlot>();
            if(t.myName==_name)
            {
                t.SetChoose();
                //获取数据
                TurnOrArtifactMainMsg msg = new TurnOrArtifactMainMsg();
                if (currName == "神器")
                {
                    msg.aData = ArtifactManager.instance.GetArtifact(t.GetComponent<TurnArtifactSlot>().myName);
                }
                else
                {
                    msg.tData = TurnManager.instance.GetTurn(t.GetComponent<TurnArtifactSlot>().myName);
                }
                EventManager.AddMsg(msg);
                return;
            }
        }
    }
    //查看是神器还是符咒
    public void JudgeArtifactTurn(params object[] par)
    {
        currName = (string)(par[0]);
        mainSlot = (TurnArtifactMainSlot)(par[2]);
        if(currName=="符咒")
        {
            currSlot = (int)(par[1]);
            CreateTurn();
        }
        else
        {
            CreateArtifact();
        }
        itemName.text=currName;
        //打开
        OpenListOne();
    }
    //实例化符咒
    public void CreateTurn()
    {
        //到所有符咒里面获取
        List<TurnDatas> datas = TurnManager.instance.datas;
        List<bool> haveItem = TurnManager.instance.haveItem;
        for(int i=0;i<datas.Count;i++)
        {
            //如果拥有，才实例化
            if (haveItem[i])
            {
                //实例化
                GameObject newSlot = Instantiate(slot);
                //设置父物体
                newSlot.transform.SetParent(listParent, false);
                //设置图片
                newSlot.transform.Find("Icon").GetComponent<Image>().sprite = datas[i].icon;
                //设置Mask
                newSlot.transform.Find("Mask").gameObject.SetActive(false);
                //进行赋值名称
                newSlot.GetComponent<TurnArtifactSlot>().SetName(datas[i].myName,currName);
                //打开
                newSlot.SetActive(true);

                slotList.Add(newSlot);
            }
        }
        for (int i = 0; i<datas.Count; i++)
        {
            //如果未拥有，才实例化
            if (!haveItem[i])
            {
                //实例化
                GameObject newSlot = Instantiate(slot);
                //设置父物体
                newSlot.transform.SetParent(listParent, false);
                //设置图片
                newSlot.transform.Find("Icon").GetComponent<Image>().sprite = datas[i].icon;
                //进行赋值名称
                newSlot.GetComponent<TurnArtifactSlot>().SetName(datas[i].myName, currName);
                //打开
                newSlot.SetActive(true);

                slotList.Add(newSlot);
            }
        }

        //限制父物体长度
        VerticalLayoutGroup lay = listParent.GetComponent<VerticalLayoutGroup>();
        RectTransform rect = listParent.GetComponent<RectTransform>();
        rect.sizeDelta = new Vector2(0, 12*(lay.spacing+100)+lay.padding.top-lay.spacing);

        //打开第一个符咒
    }
    //实例化神器
    public void CreateArtifact()
    {
        //到所有符咒里面获取
        List<ArtifactDatas> datas = ArtifactManager.instance.data;
        List<bool> haveItem = ArtifactManager.instance.haveItem;
        for (int i = 0; i < datas.Count; i++)
        {
            //如果拥有，才实例化
            if (haveItem[i])
            {
                //实例化
                GameObject newSlot = Instantiate(slot);
                //设置父物体
                newSlot.transform.SetParent(listParent, false);
                //设置图片
                newSlot.transform.Find("Icon").GetComponent<Image>().sprite = datas[i].Icon;
                //设置Mask
                newSlot.transform.Find("Mask").gameObject.SetActive(false);
                //进行赋值名称
                newSlot.GetComponent<TurnArtifactSlot>().SetName(datas[i].myName, currName);
                //打开
                newSlot.SetActive(true);

                slotList.Add(newSlot);
            }
        }
        for (int i = 0; i < datas.Count; i++)
        {
            //如果拥有，才实例化
            if (!haveItem[i])
            {
                //实例化
                GameObject newSlot = Instantiate(slot);
                //设置父物体
                newSlot.transform.SetParent(listParent, false);
                //设置图片
                newSlot.transform.Find("Icon").GetComponent<Image>().sprite = datas[i].Icon;
                //进行赋值名称
                newSlot.GetComponent<TurnArtifactSlot>().SetName(datas[i].myName, currName);
                //打开
                newSlot.SetActive(true);

                slotList.Add(newSlot);
            }
        }

        //限制父物体长度
        VerticalLayoutGroup lay = listParent.GetComponent<VerticalLayoutGroup>();
        RectTransform rect = listParent.GetComponent<RectTransform>();
        rect.sizeDelta = new Vector2(0, slotList.Count*(lay.spacing+100)+lay.padding.top-lay.spacing);

        //打开第一个符咒
    }
    //更新Main信息
    public void UpdateMainData(MsgBase msgBase)
    {
        TurnOrArtifactMainMsg msg = (TurnOrArtifactMainMsg)msgBase;

        if(currName=="神器")
        {
            nameText.text = msg.aData.myName+"神器";
            iconImage.sprite = msg.aData.Icon;
            desText.text = msg.aData.des;
            currItemName = msg.aData.myName;

            //按钮
            if(ArtifactManager.instance.SeekArtifact(currItemName))
            {
                upButton.gameObject.SetActive(false);
                downButton.gameObject.SetActive(true);
            }
            else
            {
                upButton.gameObject.SetActive(true);
                downButton.gameObject.SetActive(false);
            }
            //查看是否拥有
           if(!ArtifactManager.instance.SeekHave(currItemName))
           {
                upButton.gameObject.SetActive(false);
                downButton.gameObject.SetActive(false);
           }
        }
        else
        {
            nameText.text = msg.tData.myName + "符咒";
            iconImage.sprite = msg.tData.icon;
            desText.text = msg.tData.des;
            currItemName = msg.tData.myName;

            //按钮
            if (TurnManager.instance.SeekTurn(currItemName))
            {
                upButton.gameObject.SetActive(false);
                downButton.gameObject.SetActive(true);
            }
            else
            {
                upButton.gameObject.SetActive(true);
                downButton.gameObject.SetActive(false);
            }

            //查看是否拥有
            if (!TurnManager.instance.SeekHave(currItemName))
            {
                upButton.gameObject.SetActive(false);
                downButton.gameObject.SetActive(false);
            }
        }
    }
    //装备按钮
    public void UpButton()
    {
        if (currName == "神器")
        {
            //获取
            ArtifactDatas d=ArtifactManager.instance.GetArtifact(currItemName);
            mainSlot.OpenSlot();
            mainSlot.SetSlotIcon(d.Icon,d.myName,true,currName);
        }
        else
        {
            //获取
            TurnDatas d = TurnManager.instance.GetTurn(currItemName);
            mainSlot.OpenSlot();
            mainSlot.SetSlotIcon(d.icon,d.myName,true, currName);
        }

        //关闭装备
        upButton.gameObject.SetActive(false);
        downButton.gameObject.SetActive(true);

        TipSayManager.Instance.Say("装备 <color=yellow> " + currItemName + " </color>" + currName);

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //卸下按钮
    public void DownButton()
    {
        if (currName == "神器")
        {
            //获取
            ArtifactDatas d = ArtifactManager.instance.GetArtifact(currItemName);
            mainSlot.CloseSlot();
            mainSlot.SetSlotIcon(d.Icon, d.myName,false, currName);
        }
        else
        {
            //获取
            TurnDatas d = TurnManager.instance.GetTurn(currItemName);
            mainSlot.SetSlotIcon(d.icon, d.myName, false, currName);
        }

        //关闭装备
        upButton.gameObject.SetActive(true);
        downButton.gameObject.SetActive(false);

        TipSayManager.Instance.Say("卸下 <color=yellow> " + currItemName + " </color>" + currName);

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //关闭按钮
    public void CloseButton()
    {
        Close();

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
}
//主菜单信息
public class TurnOrArtifactMainMsg:MsgBase
{
    public TurnOrArtifactMainMsg() { proName = "TurnOrArtifactMainMsg"; }

    public ArtifactDatas aData;
    public TurnDatas tData;
}